package com.ruoyi.web.controller.revenue;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.SmsConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sms.Response;
import com.ruoyi.common.utils.sms.SendSmsUtil;
import com.ruoyi.system.domain.WaterBill;
import com.ruoyi.system.domain.WaterMeterrechargerecord;
import com.ruoyi.system.domain.vo.WaterUserMeterVO;
import com.ruoyi.system.service.IWaterBillService;
import com.ruoyi.system.service.IWaterMeterrechargerecordService;
import com.ruoyi.system.service.IWaterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户充值Controller
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/revenue/waterUserCharge")
public class WaterUserChargeController extends BaseController
{
    @Autowired
    private IWaterUserService waterUserService;
    @Autowired
    private IWaterMeterrechargerecordService waterMeterrechargerecordService;

    @Autowired
    private IWaterBillService waterBillService;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('revenue:waterUserCharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaterUserMeterVO userMeterVO)
    {
        startPage();
        List<WaterUserMeterVO> list = waterUserService.selectWaterUserMeterList(userMeterVO);
        return getDataTable(list);
    }

    /**
     * 欠费记录列表
     */
    @PreAuthorize("@ss.hasPermi('revenue:waterUserCharge:arrears')")
    @GetMapping("/arrears")
    public TableDataInfo arrears(WaterBill waterBill)
    {
        startPage();
        List<WaterBill> list = waterBillService.selectOverdueBillList(waterBill);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('revenue:waterUserCharge:sms')")
    @PostMapping("/sendSms")
    public AjaxResult sendSms(@RequestBody WaterUserMeterVO userMeterVO)
    {
//        System.err.println(userMeterVO.getContactInfo());
//        System.err.println(userMeterVO.getUserName());
//        jsonObject.put("unit_name", userMeterVO.getUserName());
//        jsonObject.put("day", "2025-09-25");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", userMeterVO.getUserName());
        jsonObject.put("code", userMeterVO.getUserId());
        jsonObject.put("count", "1");
        jsonObject.put("money","100000.00");
        jsonObject.put("number","10000.00");
        Response response = SendSmsUtil.sendInfo(SmsConstants.PHONE_NUMBER,jsonObject.toJSONString());
        if(response.getCode() == 200){
            return success();
        }
        return error("发送失败");
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('revenue:waterUserCharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        WaterUserMeterVO userMeterVO = new WaterUserMeterVO();
        userMeterVO.setUserId(id);
        userMeterVO = waterUserService.selectWaterUserMeterList(userMeterVO).get(0);
        return success(userMeterVO);
    }


    /**
     * 新增充值记录
     */
    @PreAuthorize("@ss.hasPermi('revenue:waterUserCharge:add')")
    @Log(title = "充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaterMeterrechargerecord waterMeterrechargerecord)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        waterMeterrechargerecord.setState("0");
        waterMeterrechargerecord.setOper(loginUser.getUsername());
        waterMeterrechargerecord.setCreateBy(loginUser.getUsername());
        waterMeterrechargerecord.setCompanyId(getDeptId() + "");
        return toAjax(waterMeterrechargerecordService.insertWaterMeterrechargerecord(waterMeterrechargerecord));
    }

}
